<template>
  <view class="uni-wrap">
    <scroll-view class="pb-50">
      <view class="my-card-view">
        <uni-forms ref="form" :border="true" :model="formData" :rules="rules" err-show-type="toast">
          <template>
            <uni-card title="表头" :is-shadow="false" :border="false">
              <uni-forms-item label="生产部门" name="cdeptvid" :label-width="labelWidth" required>
                <as-select
                  v-model="formData.cdeptvid"
                  title="部门选择"
                  placeholder="请选择"
                  :options="departmentList"
                  labelKey="name"
                  valueKey="code"
                />
              </uni-forms-item>
              <uni-forms-item label="仓库" name="cwarehouseid" :label-width="labelWidth" required>
                <as-select
                  v-model="formData.cwarehouseid"
                  title="仓库选择"
                  placeholder="请选择"
                  :options="warehouseList"
                  labelKey="name"
                  valueKey="code"
                />
              </uni-forms-item>
            </uni-card>
            <uni-card title="表体" :is-shadow="false" :border="false">
              <view
                class="item"
                v-for="(item, index) in formData.icFinprodinBList"
                :key="index"
                style="border-bottom: 4px solid #eee;"
              >
                <uni-forms-item
                  :label="'产成品' + (index+ 1)"
                  name="oldCode"
                  :label-width="labelWidth"
                >
                  <view class="bao-view">
                    <button type="primary" size="mini" @click="openScan(index)">扫码选择</button>

                    <view class="delete" @click="itemDelete(index)">
                      <text class="uni-card-error">删除</text>
                    </view>
                  </view>
                </uni-forms-item>

                <uni-forms-item
                  label="物料编码"
                  :name="['icFinprodinBList', index, 'cmaterialvid']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    v-model="formData.icFinprodinBList[index].cmaterialvid"
                    placeholder="扫码自动带入"
                    :disabled="true"
                    :inputBorder="false"
                  />
                </uni-forms-item>
                <uni-forms-item
                  label="物料名称"
                  :name="['icFinprodinBList', index, 'name']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    v-model="formData.icFinprodinBList[index].name"
                    placeholder="扫码自动带入"
                    :disabled="true"
                    :inputBorder="false"
                  />
                </uni-forms-item>
                <uni-forms-item
                  label="规格型号"
                  :name="['icFinprodinBList', index, 'materialspec']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    v-model="formData.icFinprodinBList[index].materialspec"
                    placeholder="扫码自动带入"
                    :disabled="true"
                    :inputBorder="false"
                  />
                </uni-forms-item>
                <uni-forms-item
                  label="含量"
                  :name="['icFinprodinBList', index, 'materialtype']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    v-model="formData.icFinprodinBList[index].materialtype"
                    placeholder="扫码自动带入"
                    :disabled="true"
                    :inputBorder="false"
                  />
                </uni-forms-item>
                <uni-forms-item
                  label="批次号"
                  :name="['icFinprodinBList', index, 'vbatchcode']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    v-model="formData.icFinprodinBList[index].vbatchcode"
                    placeholder="扫码自动带入"
                    :disabled="true"
                    :inputBorder="false"
                  />
                </uni-forms-item>

                <uni-forms-item
                  label="产生品计算"
                  :name="['icFinprodinBList', index, 'spec']"
                  :label-width="labelWidth"
                >
                  <div class="flex-row">
                    <uni-easyinput
                      v-model="formData.icFinprodinBList[index].spec"
                      :placeholderStyle="placeholderStyle"
                      placeholder="规格(kg)"
                    />
                    <text style="margin: 0 4px;">*</text>
                    <uni-easyinput
                      v-model="formData.icFinprodinBList[index].number"
                      :placeholderStyle="placeholderStyle"
                      placeholder="数量"
                    />
                  </div>
                </uni-forms-item>
             
                <uni-forms-item
                  label="应收数量(吨)"
                  :name="['icFinprodinBList', index, 'nshouldnum']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    :value="calculation(formData.icFinprodinBList[index].spec, formData.icFinprodinBList[index].number, formData.icFinprodinBList[index])"
                    :placeholderStyle="placeholderStyle"
                    placeholder="自动计算"
                  />
                </uni-forms-item>
                <uni-forms-item
                  label="实收数量"
                  :name="['icFinprodinBList', index, 'nassistnum']"
                  :label-width="labelWidth"
                >
                  <uni-easyinput
                    :value="calculation(formData.icFinprodinBList[index].spec, formData.icFinprodinBList[index].number, formData.icFinprodinBList[index])"
                    :placeholderStyle="placeholderStyle"
                    placeholder="自动计算"
                  />
                </uni-forms-item>
              </view>

              <view class="add-view">
                <button type="primary" size="mini" @click="itemAdd()" class="add-btn">+ 添加一行</button>
              </view>
            </uni-card>
          </template>
        </uni-forms>
      </view>
    </scroll-view>

    <view class="bottomFixed">
      <view class="btn-box">
        <button type="primary" class="btn-main" @click="submit('form')">确定</button>
      </view>
    </view>
  </view>
</template>

<script>
import moment from "moment";
import zqsSelect from "../../uni_modules/zqs-select/zqs-select.vue";
import { Decrypt } from "../../common/util";

export default {
  components: {
    zqsSelect
  },
  data() {
    return {
      placeholderStyle: "font-size: 14px",
      labelWidth: 100,
      formData: {
        cwarehouseid: "",
        cdeptvid: "",
        icFinprodinBList: []
      },
      rules: {
        cdeptvid: {
          rules: [
            {
              required: true,
              errorMessage: "请选择生产部门"
            }
          ]
        },
        cwarehouseid: {
          rules: [
            {
              required: true,
              errorMessage: "请选择仓库"
            }
          ]
        }
      },
      departmentList: [
        {
          label: "部门一",
          value: "1"
        },
        {
          label: "部门二",
          value: "2"
        },
        {
          label: "部门三",
          value: "3"
        }
      ],
      warehouseList: [
        {
          label: "仓库一",
          value: "1"
        },
        {
          label: "仓库二",
          value: "2"
        },
        {
          label: "仓库三",
          value: "3"
        }
      ]
    };
  },
  onLoad() {
    this.itemAdd();
    this.getDepartmentList();
    this.getWarehouseList();

    // const str =
    //   "lqSmwBK2lu0rKxZzlO9QoORKPUcGp9Y+B+pLv4prkK5xiTdzfXsV0bgBNGBOFd+Zsc1sk/FdB5VRP8XHorST4+1LoUiMbR0yVcT3idJh7tW95e7o3yQP+lrlceELZLPoZV9g2hYFQxZvTbT6vWHINE9MAOS6h/Hio0LWXTcqHG1Hh8rtHG2XK4wZ00IKlNqB";
    // console.log(Decrypt(str));
  },
  methods: {
    calculation(spec, num, row) {
      console.log("calc", spec, num);
      let value = (spec * num) / 1000;
      row.nshouldnum = value;
      row.nassistnum = value;
      return value;
    },
    //添加行
    itemAdd() {
      let obj = {
        partId: "",
        cmaterialvid: "", //物料编号
        name: "", //物料名称
        materialspec: "", //规格型号
        materialtype: "", //规格型号
        vbatchcode: "", //批次号
        nshouldnum: 0, //应收主数量
        nassistnum: 0 //实收数量
      };
      this.formData.icFinprodinBList.push(obj);
    },

    itemDelete(index) {
      this.formData.icFinprodinBList.splice(index, 1);
    },

    getDepartmentList() {
      this.$post("/nccloud/getDept", {}).then(res => {
        this.departmentList = res.data;
      });
    },

    getWarehouseList() {
      this.$post("/nccloud/getStordoc", {}).then(res => {
        this.warehouseList = res.data;
      });
    },

    //扫码
    openScan(index) {
      let that = this;
      // 允许从相机和相册扫码
      uni.scanCode({
        success: function(res) {
          console.log("条码内容：条码内容：" + res.result);
          let result = JSON.parse(res.result && Decrypt(res.result));
          console.log("解密后，", result);
          if (result) {
            console.log("result.code", result.code);
            that.formData.icFinprodinBList[index].cmaterialvid = result.code;
            that.formData.icFinprodinBList[index].name = result.name;
            that.formData.icFinprodinBList[index].materialspec =
              result.materialspec;
            that.formData.icFinprodinBList[index].materialtype =
              result.materialtype;
            that.formData.icFinprodinBList[index].vbatchcode =
              result.vbatchcode;
          }
        }
      });
    },

    goToChooseDevice() {
      let that = this;
      uni.navigateTo({
        url: "/pagesCommon/common/productChoose?workshopCode=1",
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromPage: function(res) {
            const deviceCode = res.data;
            that.getDeviceInfoByCode(deviceCode);
          }
        }
      });
    },

    //根据钢瓶编码获取钢瓶相关信息
    getDeviceInfoByCode(bottlecode) {
      let that = this;
      that
        .$post(`/basicData/steelcyinder/steelcyinderByCode`, {
          steelcyinderCode: bottlecode,
          workshopCode: "1"
        })
        .then(res => {
          let bottleInfo = res.data;
          console.log("bottleInfo before", bottleInfo);
          bottleInfo.steelcyinderId = bottleInfo.id;
          bottleInfo.steelcyinderState = "待入库";
          that.haveChooseDevice = true;
          console.log("bottleInfo after", bottleInfo);
          that.bottleAdd(bottleInfo);
        });
    },

    submit(ref) {
      this.$refs[ref].validate().then(formDatas => {
        uni.showLoading({
          title: "提交中...",
          mask: true
        });
        let subParams = {
          icFinprodinH: {
            cwarehouseid: this.formData.cwarehouseid,
            cdeptvid: this.formData.cdeptvid
          },
          icFinprodinBList: this.formData.icFinprodinBList
        };

        let url = "/nccloud/productin/save";
        console.log(subParams);
        this.$post(url, subParams).then(res => {
          uni.hideLoading();
          if (res && res.code == 200) {
            uni.showModal({
              title: "提示",
              content: "添加入库单成功",
              success: function() {
                uni.navigateBack({
                  delta: 1
                });
              }
            });
          } else {
            uni.showToast({
              title: res.msg,
              icon: "none"
            });
          }
        });
      });
    }
  }
};
</script>

<style lang="scss">
.add-view {
  margin-bottom: 20rpx;
  .sub-list {
    .item {
      margin-bottom: 8px;
      .header-view {
        display: flex;
        background: #e9edfc;
        align-items: center;
        padding: 6rpx 18rpx;
        .header-title {
          border-radius: 10rpx 10rpx 0rpx 0rpx;
          font-size: 24rpx;
          color: #78797b;
          flex: 1;
        }
      }
    }
  }
}

.bao-view {
  display: flex;
  align-items: center;
  justify-content: space-between;

  uni-button {
    margin: 0;
  }
}
// .add-view {
//   display: flex;
//   align-items: center;
//   justify-content: space-between;
// }
.add-btn {
  margin: 10px 0;
}

.flex-row {
  display: flex;
  align-items: center;
}
</style>
